package com.cskaoyan.service;



import com.cskaoyan.bean.market.Issue;
import com.cskaoyan.bean.market.IssueExample;
import com.cskaoyan.bean.market.IssueList;
import com.cskaoyan.bean.BaseParam;
import com.cskaoyan.bean.market.IssueCreateVo;

import com.cskaoyan.mapper.IssueMapper;
import com.cskaoyan.util.PageUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class IssueServiceImpl implements IssueService {

    @Autowired
    IssueMapper issueMapper;

    @Override
    public IssueList list(BaseParam baseParam, String question) {
        PageUtil.start(baseParam);
        IssueExample issueExample = new IssueExample();
        issueExample.setOrderByClause(baseParam.getSort() + " " + baseParam.getOrder());
        if(question != null && !"".equals(question)){
            issueExample.createCriteria().andQuestionLike("%" + question + "%");
        }
        List<Issue> issues = issueMapper.selectByExample(issueExample);
        ArrayList<Issue> issues1 = new ArrayList<>();
        for (Issue issue : issues) {
            if(issue.getDeleted() == false){
                issues1.add(issue);
            }
        }
        PageInfo<Issue> issuePageInfo = new PageInfo<>(issues1);
        long total = issuePageInfo.getTotal();
        int pages = issuePageInfo.getPages();
        int page = issuePageInfo.getPageNum();
        IssueList issueList = new IssueList();
        issueList.setTotal((int) total);
        issueList.setPages(pages);
        issueList.setLimit((int) total);
        issueList.setPage(page);
        issueList.setList(issues1);
        return issueList;
    }

    @Override
    public IssueCreateVo create(Map map) {
        String question = (String) map.get("question");
        /*IssueExample issueExample1 = new IssueExample();
        issueExample1.createCriteria().andQuestionEqualTo(question).andDeletedEqualTo(false);
        List<Issue> issues1 = issueMapper.selectByExample(issueExample1);
        if(issues1.size() == 1){
            return null;
        }*/
        String answer = (String) map.get("answer");
        Issue issue = new Issue();
        issue.setAnswer(answer);
        issue.setQuestion(question);
        Date time = new Date();
        issue.setAddTime(time);
        issue.setUpdateTime(time);
        issueMapper.insertSelective(issue);
        Integer id = issue.getId();
        IssueCreateVo issueCreateVo = new IssueCreateVo();
        issueCreateVo.setId(id);
        issueCreateVo.setQuestion(question);
        issueCreateVo.setAnswer(answer);
        issueCreateVo.setAddTime(time);
        issueCreateVo.setUpdateTime(time);
        return issueCreateVo;
    }

    @Override
    public IssueCreateVo update(Issue issue) {
        IssueExample issueExample = new IssueExample();
        Date updatetime = new Date();
        issue.setUpdateTime(updatetime);
        issue.setAnswer(issue.getAnswer());
        issueExample.createCriteria().andIdEqualTo(issue.getId());
        issueMapper.updateByExampleSelective(issue,issueExample);
        IssueCreateVo issueCreateVo = new IssueCreateVo();
        issueCreateVo.setId(issue.getId());
        issueCreateVo.setAnswer(issue.getAnswer());
        issueCreateVo.setQuestion(issue.getQuestion());
        issueCreateVo.setAddTime(issue.getAddTime());
        issueCreateVo.setUpdateTime(updatetime);
        return issueCreateVo;
    }

    @Override
    public void delete(Issue issue) {
        Issue issue1 = new Issue();
        Date updateTime = new Date();
        issue1.setUpdateTime(updateTime);
        issue1.setDeleted(true);
        issue1.setId(issue.getId());
        issueMapper.updateByPrimaryKeySelective(issue1);
    }
}
